﻿using System;
using System.Text;
using System.Web;
using System.Web.UI;
using NopSolutions.NopCommerce.Common.Utils;
using NopSolutions.NopCommerce.Common.Utils.Html;

namespace NopSolutions.NopCommerce.Web.Administration.Modules
{
    public partial class MenuControl : BaseNopAdministrationUserControl
    {
        private string _siteMapProviderName;

        protected override void OnInit(EventArgs e) {
            base.OnInit(e);

            Load += new EventHandler(MenuControl_Load);
        }

        void MenuControl_Load(object sender, EventArgs e) {
            SiteMapProvider siteMapProvider = SiteMap.Providers[SiteMapProviderName];
            if (siteMapProvider == null) {
                Visible = false;
            } else {
                var sb = new StringBuilder();
                sb.AppendFormat("<ul id=\"{0}\" class=\"sf-menu nav\">", ClientID);
                foreach (SiteMapNode node in siteMapProvider.RootNode.ChildNodes) {
                    BuildMenuRecursive(sb, node, 0);
                }
                sb.Append("</ul>");
                lblMenuContent.Text = sb.ToString();
            }
        }

        private void BuildMenuRecursive(StringBuilder sb, SiteMapNode node, int level) {
            string imgUrl = node["IconUrl"];
            if (!String.IsNullOrEmpty(imgUrl) && imgUrl.StartsWith("~/")) {
                imgUrl = imgUrl.Substring(2, imgUrl.Length - 2);
                imgUrl = CommonHelper.GetStoreLocation() + imgUrl;
            }

            string title = HttpUtility.HtmlEncode(!String.IsNullOrEmpty(node["nopResourceTitle"]) ? GetLocaleResourceString(node["nopResourceTitle"]) : node.Title);
            string descr = HttpUtility.HtmlEncode(!String.IsNullOrEmpty(node["nopResourceDescription"]) ? GetLocaleResourceString(node["nopResourceDescription"]) : node.Description);
            string dataToggle = level == 0 ? "data-toggle=\"dropdown\"" : string.Empty;

            sb.AppendFormat("<li class=\"{0} {1}\">", (SiteMap.CurrentNode == node ? "active" : string.Empty), (node.HasChildNodes ? level == 0 ? "dropdown" : "dropdown-submenu" : string.Empty));
            sb.AppendFormat("<a class=\"dropdown-toggle\" href=\"{0}\" title=\"{2}\" {5}>{3}{1}{4}</a>",
                (String.IsNullOrEmpty(node.Url) ? "#" : node.Url),
                title,
                descr,
                (!String.IsNullOrEmpty(imgUrl) ? String.Format("<i class=\"" + imgUrl + "\"></i> ") : String.Empty),
                (node.HasChildNodes ? level == 0 ? " <b class=\"caret\"></b>" : string.Empty : string.Empty),
                dataToggle);
            if (node.HasChildNodes) {
                sb.Append("<ul class=\"dropdown-menu\">");
                foreach (SiteMapNode childNode in node.ChildNodes) {
                    BuildMenuRecursive(sb, childNode, ++level);
                }
                sb.Append("</ul>");
            }
            sb.Append("</li>");
        }

        public string SiteMapProviderName {
            get {
                return _siteMapProviderName;
            }
            set {
                _siteMapProviderName = value;
            }
        }
    }
}